//var dimensions = [ "Human Phenotype", "Mammalian Phenotype" ];

/* 
 * retrieved the data from the KEGG page source:
 * http://www.genome.jp/kegg-bin/show_pathway?hsa05012
 * using regex page: http://gskinner.com/RegExr/ 
 * regex: <area shape="(rect|circle)" coords="(.+?)" href=".+?" title="(\d.+)\((.+?)\)">
 * replace: ["$4",$2],
 * 
 * 
 * data:  name, x, y, 46 wide, 17 high
[LRRK2,503,685,549,702],
[UBA7,248,174,294,191],
[UBB,248,204,294,221],
[UBE2L6,343,174,389,191],
[UBB,343,204,389,221],
[PARK2,514,216,560,233],
[SEPT5,620,343,666,360],
[SNCAIP,620,374,666,391],
[GPR37,621,311,667,328],
[PARK7,683,709,729,726],
[PINK1,503,710,549,727],
[SNCA,704,275,750,292],
[UCHL1,342,429,388,446],
[CYCS,809,749,855,766],
[NDUFA4L2,384,729,430,746],
[SDHD,384,757,430,774],
[UQCRH,384,786,430,803],
[COX5A,384,814,430,831],
[ATP5O,384,843,430,860],
[HTRA2,698,749,744,766],
[SLC25A31,710,811,756,828],
[UBB,429,384,475,401],
[UBB,429,412,475,429],
[UBB,429,439,475,456],
[UBB,253,384,299,401],
[UBB,253,412,299,429],
[UBB,253,439,299,456],
[UBB,524,272,570,289],
[UBB,524,303,570,320],
[UBB,523,333,569,350],
[UBE2G2,343,265,389,282],
[TH,678,461,724,478],
[SLC6A3,131,488,177,505],
[CASP9,951,748,997,765],
[CASP3,950,700,996,717],
[APAF1,871,779,917,796],
[SLC18A2,424,602,470,619],
[SLC6A3,132,691,178,708],
 */

var data = [["LRRK2",503,685,549,702,0,0],
            ["UBA7",248,174,294,191,0,0],
            ["UBB",248,204,294,221,0,0],
            ["UBE2L6",343,174,389,191,0,0],
            ["UBB",343,204,389,221,0,0],
            ["PARK2",514,216,560,233,0,0],
            ["Park2",514,216,560,233,1,1],

            ["SEPT5",620,343,666,360,0,0],
            ["SNCAIP",620,374,666,391,0,0],
            ["GPR37",621,311,667,328,0,0],
            ["PARK7",683,709,729,726,0,0],
            ["PINK1",503,710,549,727,0,0],
            ["Snca",704,275,750,292,1,0],
            ["Uchl1",342,429,388,446,1,0],

            ["Snca",704,275,750,292,1,1],
            ["Uchl1",342,429,388,446,1,1],

            ["CYCS",809,749,855,766,0,0],
            ["NDUFA4L2",384,729,430,746,0,0],
            ["SDHD",384,757,430,774,0,0],
            ["UQCRH",384,786,430,803,0,0],
            ["COX5A",384,814,430,831,0,0],
            ["ATP5O",384,843,430,860,0,0],
            ["HTRA2",698,749,744,766,0,0],
            ["SLC25A31",710,811,756,828,0,0],
            ["UBB",429,384,475,401,0,0],
            ["UBB",429,412,475,429,0,0],
            ["UBB",429,439,475,456,0,0],
            ["UBB",253,384,299,401,0,0],
            ["UBB",253,412,299,429,0,0],
            ["UBB",253,439,299,456,0,0],
            ["UBB",524,272,570,289,0,0],
            ["UBB",524,303,570,320,0,0],
            ["UBB",523,333,569,350,0,0],
            ["UBE2G2",343,265,389,282,0,0],
            ["th",678,461,724,478,1,0],           
            ["Slc6a3",131,488,177,505, 1,0],

            ["th",678,461,724,478,1,1],           
            ["Slc6a3",131,488,177,505, 1,1],

            ["CASP9",951,748,997,765,0,0],
            ["CASP3",950,700,996,717,0,0],
            ["APAF1",871,779,917,796,0,0],
            
            ["Slc18a2",424,602,470,619,1,0],
            ["Slc6a3",132,691,178,708,1,0],

			["Slc18a2",424,602,470,619,1,1],
			["Slc6a3",132,691,178,708,1,1]];


/*
 * MGI_3758072	Uchl1<gad>/Uchl1<gad>; Uchl3<tm1Tilg>/Uchl3<tm1Tilg>	MGI_103149	Uchl1
MGI_3758030	Slc18a2<tm2Kmo>/Slc18a2<tm2Kmo>	MGI_106677	Slc18a2
MGI_5441517	Slc6a3<+>/Slc6a3<tm1.1(cre)Bkmn>; Mfn2<tm3Dcc>/Mfn2<tm3Dcc>; Gt(ROSA)26Sor<+>/Gt(ROSA)26Sor<tm1.1(CAG-COX8A/Dendra2)Dcc>	MGI_94862	Slc6a3
MGI_4421411	Snca<tm1Nbm>/Snca<tm1Nbm>	MGI_1277151	Snca
MGI_5440841	Slc6a3<+>/Slc6a3<tm1(cre)Lrsn>; Mfn2<tm1.1Arte>/Mfn2<tm1.1Arte>	MGI_94862	Slc6a3
MGI_3654636	Dbh<+>/Dbh<tm2(Th)Rpa>; Th<tm1Rpa>/Th<tm1Rpa>	MGI_98735	Th
MGI_2654522	Slc6a3<tm2Mca>/Slc6a3<tm2Mca>	MGI_94862	Slc6a3

 */

var phenotype_list = ["Bradykinesia", "Lewy Bodies", "Dysphagia", "Depression"];
var phenotype_colors = ["#44a293", "#ea763b", "#A9A9A9", "#A9A9A9"];
var unselected_color = "darkgrey";
var data_array = [];
var color_scale = d3.scale.linear().domain([0,1]).range([d3.rgb("#e5e5e5"), d3.rgb("#44a293")]);

var rect_width = 47, rect_height = 19;

var m = [ 30, 10, 10, 10 ], w = 1104, h = 969;
var svg = d3.select("body").append("svg").attr("width", w+150).attr(
		"height", h).append("g").attr("transform",
		"translate(" + 0 + "," + 0 + ")");


function loadData() {
	var temp_name_array = [];
	var temp_value_array = [];
	data.forEach(function(data_item, i) {
		var temp_object = {label: data_item[0], x: data_item[1], y: data_item[2], value: data_item[5], model_id: data_item[6]};
		data_array.push(temp_object);
 	
	});

}

function init() {
	
  var pic = svg.append("svg:image")
     .attr("class", "bckgrd")
     .attr("x", 0)
     .attr("y", 0)
     .style("fill", "blue")
     .attr("width", w)
     .attr("height", h)
     .attr("xlink:href", "images/hsa05012.png");
  
  var temp_d = ["Phenotype List"]; 
	var pheno_htext = svg.selectAll(".phenotype_header")
	     .data(temp_d);
	pheno_htext.enter().append("text")
	    .attr("class", "phenotype_header")
	    .attr("x", w+44)
	    .attr("y", 40)
	    .attr("width", 140)
	    .attr("height", 20)
	    .text(String);

}

function createRects() {

	var cover_region = svg.append("g");
	//create rectangles to cover the existing rectangles
	//this allows the color saturation to work
	var rect_text = cover_region
	   .selectAll(".data_rect1")
	   .data(data_array.filter(function (d) { return d.model_id == 0;}));
	rect_text.enter()
	   		.append("rect")
		    .attr("class", "data_rect1")
			.attr("x", function(d) {
				return d.x;
			})
			.attr("y", function(d) {
				return d.y-1;
			})
		    .attr("width", rect_width)
		    .attr("height", rect_height)
		    .style("fill", "white");

	//to avoid the rectangles underneath the other rectanlges from "bleeding through",
	//I had to place them in separate "g" regions within the SVG
	for (var idx=0;idx<2;idx++) {
		var model_region = svg.append("g");
	//create the "real" rectangles using color saturation
	var rect_text2 = model_region
	   .selectAll(".data_rect2")
	   .data(data_array.filter(function (d) { return d.model_id == idx;}));
	rect_text2.enter()
	   		.append("rect")
		    .attr("class", "data_rect2")
			.attr("x", function(d) {
				return d.x + (d.model_id * 4);
			})
			.attr("y", function(d) {
				return d.y-1 - (d.model_id * 4);
			})
			.style("stroke", "black")
			.style("opacity", function(d) {
				var retVal = 0.5;
				if (d.value == 1) {
					retVal = 1.0;
				}
				return retVal;
			})
			
			.style("stroke-width", 1)
		    .attr("width", rect_width)
		    .attr("height", rect_height)
		    .attr("fill", function(d) {
		    	if (d.value == 0) {
		    		return phenotype_colors[2];
		    	}
			  	return phenotype_colors[d.model_id];
			});
	var rect_text2 = model_region
	   .selectAll(".data_text2")
	   .data(data_array.filter(function (d) { return d.model_id == idx;}));
      rect_text2.enter().append("text")
				    .attr("class", "data_text2")
					.attr("x", function(d) {
						return d.x+3 + (d.model_id * 4);
					})
					.attr("y", function(d) {
						return d.y+12 - (d.model_id * 4);
					})
/*					.style("opacity", function(d) {
						var retVal = 0.5;
						if (d.value == 1) {
							retVal = 1.0;
						}
						return retVal;
					})*/
				    .text(function(d) {
				    	return d.label;
				    });
	}

	/*
	//create the "real" text
	var rect_text3 = svg
	   .selectAll(".data_text")
	   .data(data_array);
	rect_text3.enter()
	   		.append("text")
		    .attr("class", "data_text")
			.attr("x", function(d) {
				return d.x+3 + (d.model_id * 4);
			})
			.attr("y", function(d) {
				return d.y+12 - (d.model_id * 4);
			})
			.style("opacity", function(d) {
				var retVal = 0.5;
				if (d.value == 1) {
					retVal = 1.0;
				}
				return retVal;
			})
		    .text(function(d) {
		    	return d.label;
		    });
*/
}

function createLegend() {

	var phenotype_rect = svg.selectAll("#phenotype_rect")
	  .data(phenotype_list);
	phenotype_rect.enter()
	  .append("rect")
  .attr("class", "phenotype_rect")
  .attr("x", w+40)
  .attr("y", function(d,i) {
	  return 40 + (i*36) + 12;
  })
  .attr("width", 140)
  .attr("height", 20)
  .style("fill", function(d,i) { return d3.rgb(phenotype_colors[i]);});

	var phenotype_text = svg.selectAll("#phenotype_text")
	  .data(phenotype_list);
	phenotype_text.enter()
	  .append("text")
    .attr("class", "phenotype_text")
    .attr("x", w+44)
    .attr("y", function(d,i) {
  	  return 40 + (i*36) + 26;
    })
    .attr("width", 140)
    .attr("height", 20)
    .style("fill", "white")
    .text(String);


	
}




loadData();
init();
createRects();
createLegend();

